import Vue from 'vue'

import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/home"
  }
  ,
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/home/searchpopup',
        name: 'SearchPopup',
        component: () => import(/* webpackChunkName: "SearchPopup" */ '../views/SearchPopup.vue')
      }
    ],
    meta: {
      tabbarActiveNum: 0
    }
  },
  {
    path: '/topic',
    name: 'Topic',
    component: () => import(/* webpackChunkName: "topic" */ '../views/Topic.vue'),
    meta: {
      tabbarActiveNum: 2
    }
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import(/* webpackChunkName: "category" */ '../views/Category.vue'),
    meta: {
      tabbarActiveNum: 2
    }
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import(/* webpackChunkName: "cart" */ '../views/Cart.vue'),
    meta: {
      tabbarActiveNum: 3
    }
  },
  {
    path: '/user',
    name: 'User',
    component: () => import(/* webpackChunkName: "user" */ '../views/User.vue'),
    meta: {
      tabbarActiveNum: 4
    }
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//全局路由守卫 路由拦截 能够在每一次跳转之前都拦截下来,可以进行一些操作
router.beforeEach((to, from, next) => {
  //  to和from都是对象,内部记录了从哪里来(from.path)  ,  和去往哪儿里(同.path) 
  //next("要去往的路由") 是一个方法 必须写 相当于关口

  //什么时候执行?  路由变换之前执行!!

  //判断是不是要进入购物车
  // if (to.path == "/cart") {
  //   //判断有没有token
  //   //没有就跳往用户页面
  //   let token = localStorage.getItem("token")
  //   if (token) {
  //     next()
  //   } else {
  //     //这里不是vue的实例环境所以不能用this.$toast("请先登录")
  //     Vue.prototype.$toast("请先登录")
  //     setTimeout(() => {
  //       next("/user")
  //     }, 500);
  //     return
  //   }
  // }
  next()
})

export default router
